<template>
  <div class="login-container">

    <div class="card">
      <div class="card-header"><span>云端中控</span></div>
      <div class="card-main">


        <span class="span_inputname">账号</span>
        <el-input class="mb-20" ref="username" v-model="loginForm.username" placeholder="账号" name="username" type="text"
          tabindex="1" style="width: 100%;" />

        <span class="span_inputname">密码</span>
        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码"
          name="passwd" @keyup.enter.native="handleLogin" style="width: 100%;" class="mb-20">
          <span class="show-pwd" @click="showPwd" slot="suffix">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-input>

        <div class="flex justify-between align-center">
          <el-checkbox v-model="remeberPwd">记住密码</el-checkbox>
          <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin"
            style="min-width: 120px;">登录</el-button>
        </div>

        <div class="card_foot">
          <span>还没有账号?</span>
          <el-link type="primary" @click="toReg">点击这里注册</el-link>
        </div>



      </div>

    </div>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data() {

    return {
      loginForm: {
        username: '',
        password: ''
      },

      loading: false,
      passwordType: 'password',
      redirect: undefined,
      remeberPwd: true,

    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (!this.loginForm.username) {
        this.$message.error('请输入账号');
        return
      }
      if (!this.loginForm.password) {
        this.$message.error('请输入密码');
        return
      }
      this.loading = true
      this.$store.dispatch('user/login', this.loginForm).then(() => {
        this.$router.push({ path: this.redirect || '/count' })
        this.loading = false
      }).catch((err) => {
        this.$message.error(err);
        this.loading = false
      })

    },
    toReg(){
      this.$router.push('/reg')
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  background-image: url('../../assets/images/登录背景.png');
  background-repeat: no-repeat;
  background-position-x: 50%;
  background-position-y: 50%;
  background-size: 100%;
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      // color: $light_gray;
      // caret-color: $cursor;
      font-family: Arial, Helvetica, sans-serif;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #ffff inset !important;
        // -webkit-text-fill-color: $cursor !important;
      }
    }
  }


}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {


  .show-pwd {
    height: 100%;
    position: absolute;
    right: 5px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.card {
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 2px 12px 0px;
  box-sizing: border-box;
  color: rgb(68, 68, 68);
  width: 700px;
  max-width: 700px;
  transform: translate(-50%, -50%);
  position: absolute;
  top: 40%;
  left: 50%;
  background-image: url('../../assets/images/云端管理.png');
  background-repeat: no-repeat;
  background-origin: padding-box;
  padding: 20px 20px 20px 40px;
  background-position: 93% 60%;

  .card-header {
    box-sizing: border-box;
    height: 80px;
    line-height: 90px;
    font-weight: bold;
    font-size: 20px;
  }

  .card-main {
    box-sizing: border-box;
    max-width: 300px;
    color: #444;
    padding-top: 20px;

  }

  .card_foot {
    height: 60px;
    margin-top: 20px;
    border-top: 1px solid #dcdfe6;
    color: #888;
    font-size: 14px;

    span {
      display: inline-block;
      margin-top: 30px;
      margin-right: 10px;
    }
  }

}

.span_inputname {
  margin-bottom: 10px;
  font-size: 14px;
  color: #444;

  display: inline-block;
}

// 手机端适配
@media only screen and (max-width: 840px) {
  body {
    background-image: none;
  }
}

@media only screen and (max-width: 840px) {
  .card {
    box-shadow: none;
    box-sizing: border-box;
    padding: 0;
    color: #444;
    width: 100%;
    max-width: 100%;
    -webkit-transform: none;
    -moz-transform: none;
    -ms-transform: none;
    -o-transform: none;
    transform: none;
    position: absolute;
    top: 0;
    left: 0;
    padding-right: 20px;
    background-image: none;
    background-repeat: no-repeat;
    /* background-size: 100%; */
    background-position-x: right;
    background-position-y: center;
    background-origin: padding-box;
    background-position: 93% 60%;
  }
}

@media only screen and (max-width: 840px) {
  .card-header {
    box-sizing: border-box;
    height: 250px!important;
    width: 100%;
    text-align: center;
    background-image: url('../../assets/images/云端管理.png');
    background-repeat: no-repeat;
    background-size: 250px;
    background-position: 60% 50%;
    margin-top: 20px;
  }
}

@media only screen and (max-width: 840px) {
  .card-main {
    width: 100%!important;
    margin: 0;
    max-width: 100%!important;
    padding-left: 20px;
    padding-top: 20px;
  }
}

@media only screen and (max-width: 840px) {
  .card-header>span {
    display: none;
  }
}

</style>
